package com.maduar.demo.proxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Description:
 *
 * @author danny.pang danny.pang@ebizprise.com
 * @date 2020-03-27 00:00
 */
public class CglibSubject implements MethodInterceptor {

    private Object subject;

    public Object getInstance(Object subject) {
        this.subject = subject;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(this.subject.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        Object result = null;

        try {
            result = methodProxy.invokeSuper(o, objects);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
}
